<?php
$msg="";
function validateForm($ar) {
    $toReturn = true;
    $str = "";
    for ($i = 0; $i < count($ar); $i++) {
        if ($_POST[$ar[$i]] == "" || strlen($_POST[$ar[$i]]) < 1 || is_null($_POST[$ar[$i]])) {
            $toReturn = false;
            $str.= $ar[$i];
        }
    }
//   echo $str;
    return $toReturn;


}

 global $cquery;
 $countrys = $cquery->processQueryResult("select Code, Name from country ");
$op="";
 foreach($countrys as $country){
     $selected='';
     if($country["Code"]=="GHA") $selected=" selected='selected' ";

     $op.="<option $selected value='".$country["Code"]."'>".$country["Name"]."</option>";
 }
 function sendMail($frm,$tto,$heading,$msg) {

        if(strlen($frm)<4)
            return "Unable to Sender Mail Please Check if the CONTACT EMAIL in settings has been set";
        else {
           // global $SITE_NAME;
            $from =$frm."@".$_SERVER['HTTP_HOST'];
            $headers = "MIME-Version: 1.0\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\n";
            $headers .= "From: ADK Consortium <$from>\n";
            $headers .= "Return-Receipt-To: ".$from."\n";
            $HEADING = $heading ;

            mail($tto,$heading, mailTemplate($heading,$msg) ,$headers);
            return "Mail Successfully Sent to $tto";
        }

    }

    function mailTemplate($heading,$message) {
        global $SITE_URL,$SITE_LOGO_URL,$SITE_NAME;
        $t ="   <style type=\"text/css\">";
        $t.="	<!--";
        $t.="	#container {";
        $t.="	margin: auto;";
        $t.="	width: 700px;";
        $t.="	background-color: #FFF;";
        $t.="	font-family: Verdana, Geneva, sans-serif;";
        $t.="	}";
        $t.="body {";
        $t.="	color: #955941;";
        $t.="	background-color: #F0F0F0;";
        $t.="	line-height: 23px;";
        $t.="	font-family: Verdana, Geneva, sans-serif;";
        $t.="	font-size: 12px;";
        $t.="}";
        $t.="a {";
        $t.="	text-decoration: none;";
        $t.="	color: #333;";
        $t.="}";
        $t.="#header {";
        $t.="	background-color: #955941;";
        $t.="	padding: 5px;";
        $t.="}
             #header a{font-size:18px;color:#fff}";
        $t.="img a, a img {";
        $t.="	border-top-width: 0px;";
        $t.="	border-right-width: 0px;";
        $t.="	border-bottom-width: 0px;";
        $t.="	border-left-width: 0px;";
        $t.="	border-top-style: none;";
        $t.="	border-right-style: none;";
        $t.="	border-bottom-style: none;";
        $t.="	border-left-style: none;";
        $t.="}";
        $t.="#footer {";
        $t.="	text-align: center;";
        $t.="}";
        $t.="#messagecontent {";
        $t.="	margin: 5px;";
        $t.="	padding: 5px 10px;";
        $t.="	background-color: #F0F0F0;";
        $t.="	min-height: 100px";
        $t.="}";
        $t.="#messagetitle {";
        $t.="	font-family: Verdana, Geneva, sans-serif;";
        $t.="	font-size: 12px;line-height:30px;";
        $t.="	font-weight: bold;";
        $t.="}";
        $t.="-->";
        $t.="</style>";
        $t.="<div id=\"container\">";
        $t.="	<div id=\"header\">";
        $t.="        <a href='http:\\www.adk-consortium.com'>";
        $t.="         ADK Consortium ";
        $t.="        </a>";
        $t.="    </div>";
        $t.="    <div id=\"messagetitle\">".$heading."</div>";
        $t.="  <div id=\"messagecontent\">".$message."</div>";
        $t.="  <div id=\"footer\">
                <a href='http:\\www.adk-consortium.com' >ADK Consortium | Consulting Engineers, Architects, Surveyors</a>
                </div>";
        $t.="</div>";
        return $t;
    }
if(isset($_POST["form_action"]) && $_POST["form_action"]=="contact_us")
{
    //include 'class/MailControllerClass.php';
   $email1="deblington@yahoo.com";
   $email2="info@adk-consortium.com";
   //$email2="kwasett@gmail.com";
   $validField = array("surname","email","comment","how_u_located_us");
    if(!validateForm($validField)){
        $msg="Some Fields are Null ";
    }else{
        $emailPattern= "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
        if(ereg($emailPattern, $_POST['email'])){
         $insertSQL = sprintf("
                        INSERT INTO contact_message (sender_name, sender_email, sender_telephone, sender_comment, date_posted,info_from, country, company, profession, sender_othername) VALUES (%s, %s, %s, %s,now(), %s, %s, %s, %s, %s)",
                       $cquery->GetSQLValueString($_POST['surname'], "text"),
                       $cquery->GetSQLValueString($_POST['email'], "text"),
                       $cquery->GetSQLValueString($_POST['contact_num'], "text"),
                       $cquery->GetSQLValueString($_POST['comment'], "text"),
                       $cquery->GetSQLValueString($_POST['how_u_located_us'], "text"),
                       $cquery->GetSQLValueString($_POST['country'], "text"),
                       $cquery->GetSQLValueString($_POST['org'], "text"),
                       $cquery->GetSQLValueString($_POST['prof'], "text"),
                       $cquery->GetSQLValueString($_POST['othernames'], "text"));

         $cquery->processAllQuery($insertSQL);
         $msg=" Comment Successfully Sent You Would be contacted Soon";
         $message ="Hi ,
                    <br />A message was sent by <b>". $_POST['surname'].", ".$_POST['othernames']."</b>";
         $message.=" on ".@date("Y M, D h:i a")."
                   <br />Details<br />
                   <br  />Email Address : ".$_POST['email']."
                   <br />Telephone     : ".$_POST['contact_num']."
                   <br />Country     : ".$_POST['country']."
                   <br />Organisation     : ".$_POST['org']."
                   <br />Profession     : ".$_POST['prof'];
         $message.="
                    <br /><b>Purpose/Reason</b>
                    <br />".$_POST['comment'];
       $message.="
                    <br />
                    How did u locate us? : ".$_POST['how_u_located_us'];

//$mails =  new MailControllerClass();
@sendMail("info",$email1,"ADK-Contact Form From : ".$_POST['surname'].", ".$_POST['othernames'],$message);
@sendMail("info",$email2,"ADK-Contact Form From : ".$_POST['surname'].", ".$_POST['othernames'],$message);
@sendMail("info",$_POST["email"],"ADK-Contact Form From : ".$_POST['surname'].", ".$_POST['othernames'],$message."<br />Thank you for the mail you would be contacted soon");

@sendMail("info","kwasett@gmail.com","ADK - Contact Form Filled By: ".strtoupper($_POST['surname']).", ".$_POST['othernames'],$message);

//mail($email1, , $message);
$_POST = null;
       }else{
            $msg="Invalid Email Address";
        }
}
}
if(strlen($msg)>0) echo "<div class='redtxt'>$msg</div>";
?>
<h2 style="background:#A77763;color:#fff;padding-left:20px">Please Fill The Form to Contact us We Would Like to hear from you</h2>
<form id="form1" method="post" action="">
  <table width="560" class="form_enquiries">
    <tr>
      <td width="215" valign="top">&nbsp;</td>
      <td width="333">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top">Surname:</td>
      <td><input type="text"  class="input_text" name="surname" id="surname" value="<?php echo @$_POST["surname"]?>" /></td>
    </tr>
    <tr>
      <td valign="top">Othernames</td>
      <td><label for="othernames"></label>
        <input type="text"  class="input_text" name="othernames" id="othernames" value="<?php echo @$_POST["othernames"]?>" />
        *</td>
    </tr>
    <tr>
      <td valign="top">Contact Number</td>
      <td><input type="text"  class="input_text" name="contact_num" id="contact_num" value="<?php echo @$_POST["contact_num"]?>" /></td>
    </tr>
    <tr>
      <td valign="top">Email :</td>
      <td><input type="text"  class="input_text" name="email" id="email" value="<?php echo @$_POST["email"]?>" />
        *</td>
    </tr>
    <tr>
      <td valign="top">Profession:</td>
      <td><label for="prof"></label>
        <input type="text"  class="input_text" name="prof" id="prof" value="<?php echo @$_POST["prof"]?>" /></td>
    </tr>
    <tr>
      <td valign="top"> Organisation:<br /></td>
      <td><label for="org"></label>
        <input type="text"  class="input_text" name="org" id="org" value="<?php echo @$_POST["org"]?>" /></td>
    </tr>
    <tr>
      <td valign="top">Country:</td>
      <td><label for="country"></label>
        <select name="country" id="country" class="select"><?php echo $op; ?>
        </select></td>
    </tr>
    <tr>
      <td valign="top">Purpose / Reason :</td>
      <td><textarea name="comment" id="comment" cols="30" rows="10" class="text_area">
      <?php echo @$_POST["comment"]?>
      </textarea>
        *</td>
    </tr>
    <tr>
      <td valign="top">How did u locate us?</td>
      <td><p>
        <label>
          <input type="radio" name="how_u_located_us" value="friend" id="how_u_located_us_0" />
          Friends</label>
        <label>
          <input type="radio" name="how_u_located_us" value="online" id="how_u_located_us_1" />
          Online Ad</label>
        <label>
          <input type="radio" name="how_u_located_us" value="newspaper" id="how_u_located_us_2" />
          Newspaper</label>
        <br />
      </p></td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td><input type="submit" name="m4submit" id="m4submit" value="Contact Us" class="btn"/></td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td><input name="form_action" type="hidden" id="form_action" value="contact_us" /></td>
    </tr>
    <tr>
      <td valign="top">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
